package com.Webserver.controller;

import com.Webserver.http.HttpRequest;
import com.Webserver.http.HttpResponse;
import com.Webserver.vo.Article;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ArticleController {
    private static String articlesName = "./articles/";

    static {
        File userDir = new File(articlesName);
        if (!userDir.exists()) {
            userDir.mkdirs();
        }
    }

    public void writeArticle(HttpRequest request, HttpResponse response) {
        System.out.println("文章写入中...");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String author = request.getParameter("author");
        if (title == null || content == null || author == null) {
            response.setEntity(new File("./webapps/myweb/article_fail.html"));
            return;
        }
        Article article = new Article(title, content, author);

        try (
                ObjectOutputStream ots = new ObjectOutputStream(
                        new FileOutputStream(articlesName + title + ".obj")
                );
        ) {
            ots.writeObject(article);
            response.setEntity(new File("./webapps/myweb/article_success.html"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("文章发表处理完毕!");
    }

    public void showAllArticle(HttpRequest request, HttpResponse response) {
        System.out.println("文章列表页面生成...");

        List<Article> list = new ArrayList<>();
        File file = new File(articlesName);
        File[] files = file.listFiles(e -> e.getName().endsWith(".obj"));
        for (File arr : files) {

            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(arr));
            ) {
                Article article = (Article)ois.readObject();
                list.add(article);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        list.forEach(System.out::println);

        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"2\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("</tr>");
        for (Article article : list) {
            pw.println("<tr>");
            pw.println("<td>" + article.getTitle() + "</td>");
            pw.println("<td>" + article.getContent() + "</td>");
            pw.println("</tr>");
        }
        pw.println(" </table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        response.setContentType("text/html");
        System.out.println("文章列表页面生成完毕!!");
    }
}
